package com.castle.fortress.admin.form.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.form.dto.ColModelDto;
import com.castle.fortress.admin.form.dto.FormModelDto;
import com.castle.fortress.admin.form.mapper.FormModelMapper;
import com.castle.fortress.admin.form.service.FormModelService;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.BizErrorCode;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 表模型管理 服务实现类
 *
 * @author castle
 * @since 2021-11-10
 */
@Service
public class FormModelServiceImpl  implements FormModelService {
    @Autowired
    private FormModelMapper formModelMapper;

    @Override
    public List<String> allTableNames() {
        return formModelMapper.allTableNames();
    }

    @Override
    public boolean createTable(FormModelDto formModelDto) {
        List<String> tableNames=allTableNames();
        for(String name:tableNames){
            if(name.equals(formModelDto.getTbName())){
                throw new BizException(BizErrorCode.FORM_TABLE_EXIST_ERROR);
            }
        }
        return formModelMapper.createTable(formModelDto);
    }

    @Override
    public boolean alterTable(FormModelDto formModelDto) {
        if(CommonUtil.verifyParamNull(formModelDto,formModelDto.getTbName(),formModelDto.getCols(),formModelDto.getOldCols())
        ||formModelDto.getCols().isEmpty() || formModelDto.getOldCols().isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        List<ColModelDto> addCols=new ArrayList<>();
        List<ColModelDto> modifyCols=new ArrayList<>();
        List<ColModelDto> existCols=new ArrayList<>();
        boolean isFind = false;
        for(ColModelDto newCol:formModelDto.getCols()){
            isFind=false;
            for(ColModelDto oldCol:formModelDto.getOldCols()){
                if(newCol.getColName().equals(oldCol.getColName())){
                    isFind=true;
                    existCols.add(oldCol);
                    if(!newCol.equals(oldCol)){
                        modifyCols.add(newCol);
                    }
                    break;
                }
            }
            if(!isFind){
                addCols.add(newCol);
            }
        }
        formModelDto.getOldCols().removeAll(existCols);
        formModelDto.setDropCols(formModelDto.getOldCols());
        formModelDto.setAddCols(addCols);
        formModelDto.setModifyCols(modifyCols);
        //删除字段
        if(!formModelDto.getDropCols().isEmpty()){
            formModelMapper.alterTableDrop(formModelDto);
        }
        //修改字段
        if(!formModelDto.getModifyCols().isEmpty()){
            formModelMapper.alterTableModify(formModelDto);
        }
        //添加字段
        if(!formModelDto.getAddCols().isEmpty()){
            formModelMapper.alterTableAdd(formModelDto);
        }
        return true;
    }

    @Override
    public boolean mergeTable(FormModelDto formModelDto) {
        if(isExistTable(formModelDto.getTbName())){
            return alterTable(formModelDto);
        }else{
            return createTable(formModelDto);
        }
    }

    @Override
    public boolean dropTable(String tbName) {
        if(StrUtil.isEmpty(tbName)||(tbName.indexOf("castle_form")!=0&&tbName.indexOf("castle_approve_form")!=0)){
            return false;
        }
        if(isExistTable(tbName)){
            return formModelMapper.dropTable(tbName);
        }else{
            return true;
        }
    }

    @Override
    public boolean isExistTable(String tbName) {
        List<String> tableNames=allTableNames();
        boolean isExist=false;
        for(String name:tableNames){
            if(name.equals(tbName)){
                isExist=true;
                break;
            }
        }
        return isExist;
    }

    @Override
    public List<Map<String, Object>> queryData(String tbName, Map<String, Object> dataMap) {
        return null;
    }

    @Override
    public boolean updateData(String tbName, Map<String, Object> dataMap) {
        return false;
    }

    @Override
    public Boolean deleteData(String tbName, Map<String, Object> dataMap) {
        return null;
    }
}

